package cn.sc.allinpay.allfavour.app;

import android.content.Context;

import cn.pai.connect.http.DefaultOkHttpFactory;
import cn.pai.connect.http.OkHttp;
import cn.sc.allinpay.allfavour.base.connect.http.AppRequest;
import cn.sc.allinpay.allfavour.base.connect.socket.OkSocket;
import cn.sc.allinpay.allfavour.base.connect.socket.connectfactory.AllinpayConnectionFactory;
import cn.sc.allinpay.allfavour.base.db.DbHelper;
import cn.sc.allinpay.allfavour.business.common.data.CheckCouponsRecord;
import cn.sc.allinpay.allfavour.business.common.data.DistCouponRecord;
import cn.sc.allinpay.allfavour.business.common.data.SaleCouponRecord;
import cn.sc.allinpay.allfavour.business.common.data.TransRecord;

/**
 * 应用包装
 * author：pany
 * on 2017/11/15 23:14
 */
public class AppWrapper {

    private static class AppWrapperHolder {
        /**
         * 单例对象实例
         */
        private static final AppWrapper INSTANCE = new AppWrapper();
    }

    //防止直接通过构造函数new
    private AppWrapper() {

    }

    public static final AppWrapper getInstance() {
        return AppWrapperHolder.INSTANCE;
    }

    //一般操作UI操作使用activity的context，其他地方使用application的context。并且注意context的引用防止内存溢出
    private Context appContext;


    public void init(Context appContext) {
        this.appContext = appContext;
        DbHelper.initialize(appContext, 4, "allinpay", TransRecord.class.getName(),
                CheckCouponsRecord.class.getName(), SaleCouponRecord.class.getName(),DistCouponRecord.class.getName());
        //初始化Okhttp通信
        OkHttp.initialize(new DefaultOkHttpFactory(appContext));
        //初始化socket通信
        OkSocket.initialize(new AllinpayConnectionFactory());
    }

    public Context getAppContext() {
        return appContext;
    }

    /**
     * 网络请求
     */
    public AppRequest newRequest() {
        return new AppRequest();
    }

    /**
     * 营销前置网络请求
     *
     * @param data
     * @param onResponse
     */
    public void send(String data, OkSocket.OnResponse onResponse) {
        OkSocket.getInstance().send(data, onResponse);
    }
}
